<template>
  <div>
    <el-dialog :close-on-click-modal="false" :visible.sync="visible" @closed="visible = false">
      <el-dialog width="40%" title="选择属性" :visible.sync="innerVisible" append-to-body>
        <div>
          <el-table
            :data="dataList"
            v-loading="loading"
            @selection-change="innerSelectionChangeHandle"
            style="width: 100%;"
          >
            <el-table-column type="selection" header-align="center" align="center"></el-table-column>
            <el-table-column prop="name" header-align="center" align="center" label="属性名"></el-table-column>
            <el-table-column prop="valueSelect" header-align="center" align="center" label="可选值列表"
            ></el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNo"
            :limit.sync="queryParams.pageSize"
            @pagination="loadUncorrelatedList"
          />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="mini" icon="el-icon-circle-check" @click="submitAddRelation">确认</el-button>
          <el-button size="mini" icon="el-icon-circle-close" @click="innerVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <el-row>
        <el-col :span="24">
          <el-button size="mini" plain icon="el-icon-circle-plus-outline" type="primary" @click="addRelation">新建关联</el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            plain
            @click="batchDeleteRelation"
            :disabled="selections.length <= 0"
          >批量删除
          </el-button>

          <el-table
            :data="relationAttrs"
            style="width: 100%;margin-top: 10px"
            @selection-change="selectionChangeHandle"
          >
            <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
            <el-table-column align="center" prop="name" label="属性名"></el-table-column>
            <el-table-column align="center" prop="valueSelect" label="可选值">
              <template slot-scope="scope">
                <el-tag style="margin: 0 3px" v-for="(item,index) in scope.row.optionalValue" :key="index" size="small">{{ item }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button type="text" icon="el-icon-delete" size="small" @click="relationRemove(scope.row)">移除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>

import {addAttrAttrGroupRelation, deleteAttrAttrGroupRelation, getRelationByGroupId, noRelationByGroupId} from '@/api/product/attrGroup'

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      groupId: 0,
      visible: false,
      innerVisible: false,
      relationAttrs: [],
      selections: [],
      total: 0,
      dataList: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      loading: false,
      innerSelections: []
    }
  },
  methods: {
    selectionChangeHandle(val) {
      this.selections = val
    },
    innerSelectionChangeHandle(val) {
      this.innerSelections = val
    },
    addRelation() {
      this.loadUncorrelatedList()
      this.innerVisible = true
    },
    batchDeleteRelation() {
      let postData = []
      this.selections.forEach(item => {
        postData.push({ attrId: item.attrId, groupId: this.groupId })
      })
      deleteAttrAttrGroupRelation(postData).then(res => {
        if (res.success) {
          this.$message({ type: 'success', message: '删除成功' })
        } else {
          this.$message({ type: 'error', message: res.msg })
        }
      })
    },
    //移除关联
    relationRemove(row) {
      let data = [
        {
          attrId:row.id,
          attrGroupId:this.groupId
        }
      ]
      deleteAttrAttrGroupRelation(data).then(res => {
        if (res.success) {
          this.$message({ type: 'success', message: '删除成功' })
          this.loadRelationAttrs()
        }
      })
    },
    submitAddRelation() {
      this.innerVisible = false
      if (this.innerSelections.length > 0) {
        let postData = []
        this.innerSelections.forEach(item => {
          postData.push({
            attrId: item.id,
            attrGroupId: this.groupId
          })
        })
        addAttrAttrGroupRelation(postData).then(res => {
          if (res.success) {
            this.$message({ type: 'success', message: '新增关联成功' })
          }
          this.$emit('ok')
          this.loadRelationAttrs()
        })
      }
    },
    add(id) {
      this.groupId = id
      this.queryParams.id = id
      this.visible = true
      this.loadRelationAttrs()
    },
    loadRelationAttrs() {
      getRelationByGroupId(this.groupId).then(res => {
        this.relationAttrs = res.data
      })
    },
    loadUncorrelatedList() {
      this.loading = true
      noRelationByGroupId(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records
          this.total = res.data.total
        }
        this.loading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.loadUncorrelatedList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.loadUncorrelatedList()
    }
  }
}
</script>
<style scoped>
</style>
